package com.tanhua.server.service;

import com.tanhua.dubbo.api.db.*;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Version 1.0
 * @Author aao
 * @Date 2022/2/21 15:35
 * @Description 测灵魂业务层
 */
@Service
public class TestSoulService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private QuestionnaireApi questionnaireApi;

    @DubboReference
    private ReportApi reportApi;

    @DubboReference
    private UserQuestionApi userQuestionApi;

    @DubboReference
    private OptionApi optionApi;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 测灵魂-查看结果
     * @param reportId
     * @return
     */
    public ReportVo findReport(Long reportId) {
        //1.查询报告表 获取报告信息
        Report report = reportApi.findById(reportId);
        //2.根据当前报告查询报告表获取 与你相似用户的id列表(随机取10条)
        List<Long> userIdList = reportApi.findSimilarYouIds(report);
        //3.查询用户信息 导入vo返回
        List<SimilarYouVo> similarYouVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userIdList)) {
            List<UserInfo> userInfoList = userInfoApi.findUsersByBatchIds(userIdList);
            similarYouVoList = userInfoList.stream().map(SimilarYouVo::init).collect(Collectors.toList());
        }
        return ReportVo.init(report, similarYouVoList);
    }

    /**
     * 测灵魂-问卷列表
     * @return
     */
    public List<QuestionnaireVo> findQuestionnaireList() {
        //1.获取问卷列表
        Long userId = UserHolder.getUserId();
        List<Questionnaire> questionnaireList = questionnaireApi.findQuestionnaireList();
        //非空判断
        if (CollectionUtils.isEmpty(questionnaireList)) {
            return null;
        }
        //2.创建questionnaireVoList空集合 遍历循环questionnaireList
        List<QuestionnaireVo> questionnaireVoList = new ArrayList<>();
        for (Questionnaire questionnaire : questionnaireList) {
            Long questionnaireId = questionnaire.getId();
            //2.1根据问卷id和用户id查询报告表
            Report report = reportApi.findByQuestionnaireIdAndUserId(questionnaireId,userId);
            //2.2根据问卷id查询问题列表 遍历问题集合导入选项
            List<UserQuestion> userQuestionList = userQuestionApi.findByQuestionnaireId(questionnaireId);
            List<UserQuestionVo> userQuestionVoList = new ArrayList<>();
            for (UserQuestion userQuestion : userQuestionList) {
                //2.2.1根据问题id查询选项列表
                List<Option> optionList = optionApi.findByUserQuestionId(userQuestion.getQuestionId());
                List<OptionVo> optionVoList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(optionList)) {
                    optionVoList = optionList.stream().map(OptionVo::init).collect(Collectors.toList());
                }
                //2.2.2合并问题和选项
                UserQuestionVo userQuestionVo = UserQuestionVo.init(userQuestion, optionVoList);
                userQuestionVoList.add(userQuestionVo);
            }
            //2.3获取questionnaireVo 加入集合
            QuestionnaireVo questionnaireVo = QuestionnaireVo.init(questionnaire, report, userQuestionVoList);
            String key = "QUESTIONNAIRE_LOCK_" + userId;
            String hashKey = questionnaire.getLevel().toString();
            //2.4如果存在 当前用户当前问卷level的缓存：锁定状态设置为解锁否则默认锁住
            if (!StringUtils.isEmpty(stringRedisTemplate.opsForHash().get(key,hashKey))) {
                questionnaireVo.setIsLock(0);
            }
            questionnaireVoList.add(questionnaireVo);
        }
        return questionnaireVoList;
    }
}
